package pulpfizz.pulp.body;

import org.jbox2d.collision.AABB;
import org.jbox2d.dynamics.Body;
import pulpcore.image.CoreGraphics;
import pulpcore.image.CoreImage;
import pulpcore.math.CoreMath;

/**
 * Draws Body objects using an image loaded from a file.
 * Created by will
 * Date: Apr 23, 2008 6:22:44 AM
 */
public class ImageBodySprite extends BodySpriteOld {

    private CoreImage image;
    
    public ImageBodySprite(Body body, PhysicsLayer physics, String imageAsset, AABB aabb) {
    	super(body, physics, aabb);
    	createCoreImage(imageAsset);
    }
    
    public ImageBodySprite(Body body, PhysicsLayer physics, CoreImage imageAsset, AABB aabb) {
    	super(body, physics, aabb);
    	setImage(imageAsset);
    }
    
    public ImageBodySprite(Body body, PhysicsLayer physics, CoreImage imageAsset) {
    	super(body, physics);
    	setImage(imageAsset);
    }

    public ImageBodySprite(Body body, PhysicsLayer physics, String imageAsset) {
        super(body, physics);
        createCoreImage(imageAsset);
    }

    public ImageBodySprite(Body body, PhysicsLayer physics) {
        this(body,physics,"");
    }

    private void createCoreImage(String imageAsset) {
        CoreImage ci = CoreImage.load(imageAsset);
        this.setImage(ci);
        this.setSize(width.get(),height.get());
        this.image.setHotspot(ci.getWidth()/2,ci.getHeight()/2);
    }

    /** (from ImageSprite) */
    public void setImage(CoreImage image) {
        if (this.image != image) {
            this.image = image;
            setDirty(true);
        }
    }

    /** (from ImageSprite) */
    public void update(int elapsedTime) {
        super.update(elapsedTime);

        if (image != null) {
            boolean changed = image.update(elapsedTime);
            if (changed) {
                setDirty(true);
            }
        }
    }

    /** (from ImageSprite) */
    protected void drawSprite(CoreGraphics g) {
        if (image != null) {
            g.drawImage(image);
        }
    }

    /** (from ImageSprite) */
    protected int getNaturalWidth() {
        if (image != null) {
            return CoreMath.toFixed(image.getWidth());
        }
        else {
            return super.getNaturalWidth();
        }
    }

    /** (from ImageSprite) */
    protected int getNaturalHeight() {
        if (image != null) {
            return CoreMath.toFixed(image.getHeight());
        }
        else {
            return super.getNaturalHeight();
        }
    }

    /** (from ImageSprite) */
    protected int getAnchorX() {
        if (image != null && getAnchor() == DEFAULT) {
            return CoreMath.toFixed(image.getHotspotX());
        }
        else {
            return super.getAnchorX();
        }
    }

    /** (from ImageSprite) */
    protected int getAnchorY() {
        if (image != null && getAnchor() == DEFAULT) {
            return CoreMath.toFixed(image.getHotspotY());
        }
        else {
            return super.getAnchorY();
        }
    }

}